﻿using LiteFramework.Enum;
using System;

namespace LiteFramework.Model
{
    /// <summary>
    /// 用户注册申请信息
    /// </summary>
    [Serializable]
    public sealed class RegInfo : Entity<RegInfo>
    {
        public RegInfo()
        {
        }

        public LoginType LoginType
        {
            get { return this.GetValue<LoginType>("LoginType"); }
            set { this.SetValue("LoginType", value); }
        }
        public string PrimeTag
        {
            get { return this.GetValue<string>("PrimeTag"); }
            set { this.SetValue("PrimeTag", value); }
        }
        /// <summary>
        /// 第三方授权的应用ID。LoginType不等于LoginType.Default时有效
        /// </summary>
        [Obsolete("此值改为从应用程序的配置文件中读取")]
        private string AppId
        {
            get { return this.GetValue<string>("AppId"); }
            set { this.SetValue("AppId", value); }
        }
        /// <summary>
        /// 用户OpenId。LoginType不等于LoginType.Default时有效
        /// </summary>
        public string OpenId
        {
            get { return this.GetValue<string>("OpenId"); }
            set { this.SetValue("OpenId", value); }
        }
        /// <summary>
        /// 用户Token。LoginType不等于LoginType.Default时有效
        /// </summary>
        public string AccessToken
        {
            get { return this.GetValue<string>("AccessToken"); }
            set { this.SetValue("AccessToken", value); }
        }
        /// <summary>
        /// 用户授权码。LoginType不等于LoginType.Default时有效
        /// </summary>
        public string AuthorizationCode
        {
            get { return this.GetValue<string>("AuthorizationCode"); }
            set { this.SetValue("AuthorizationCode", value); }
        }
        public string UserName
        {
            get
            {
                string userName = this.GetValue<string>("UserName");
                if (this.LoginType == LoginType.Default)
                {
                    if (string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(this.OpenId)) // 用户名为空，且Token存在，则基于Token生成用户名
                    {
                        string prefix = this.LoginType.ToString() + "_";
                        int appentLen = 16 - prefix.Length;// 16: 用户名的最大长度
                        userName = prefix + this.OpenId.Substring(Math.Max(0, this.OpenId.Length - appentLen));
                    }
                }

                return userName;
            }
            set { this.SetValue("UserName", value); }
        }
        public string Password
        {
            get
            {
                string password = this.GetValue<string>("Password");
                if (this.LoginType != LoginType.Default)
                {
                    if (string.IsNullOrEmpty(password))
                    {
                        password = Guid.NewGuid().ToString("N").Substring(26);// 通过第三方授权注册，用户密码设为随机密码，6位
                        this.SetValue("Password", password);
                    }
                }

                return password;
            }
            set { this.SetValue("Password", value); }
        }
        public string RealName
        {
            get { return this.GetValue<string>("RealName"); }
            set { this.SetValue("RealName", value); }
        }
        public string NickName
        {
            get { return this.GetValue<string>("NickName"); }
            set { this.SetValue("NickName", value); }
        }
        public Gender Gender
        {
            get { return this.GetValue<Gender>("Gender"); }
            set { this.SetValue("Gender", value); }
        }
        public string HeadImgURL
        {
            get { return this.GetValue<string>("HeadImgURL"); }
            set { this.SetValue("HeadImgURL", value); }
        }
        public string Mobile
        {
            get { return this.GetValue<string>("Mobile"); }
            set { this.SetValue("Mobile", value); }
        }
        public string Email
        {
            get { return this.GetValue<string>("Email"); }
            set { this.SetValue("Email", value); }
        }
        public string Company
        {
            get { return this.GetValue<string>("Company"); }
            set { this.SetValue("Company", value); }
        }
        public string Remark
        {
            get { return this.GetValue<string>("Remark"); }
            set { this.SetValue("Remark", value); }
        }

        public bool AutoLogin
        {
            get { return this.GetValue<bool>("AutoLogin"); }
            set { this.SetValue("AutoLogin", value); }
        }

        /// <summary>
        /// 转为用户信息
        /// </summary>
        /// <returns></returns>
        public User ToUser()
        {
            User user = new User
            {
                UserType = UserType.Member, // 强制设置为注册用户
                LastLoginType = this.LoginType,
                UserName = this.UserName,
                InitPassword = this.Password,
                RealName = this.RealName,
                NickName = this.NickName,
                Gender = this.Gender,
                HeadImgURL = this.HeadImgURL,
                Mobile = this.Mobile,
                Email = this.Email,
                Remark = this.Remark,
            };

            user.setOpenId(this.LoginType, this.OpenId);

            return user;
        }

        /// <summary>
        /// 从LoginInfo转换
        /// </summary>
        /// <param name="loginInfo"></param>
        /// <returns></returns>
        public static RegInfo From(LoginInfo loginInfo)
        {
            if (loginInfo == null)
                throw new ArgumentNullException("loginInfo");

            if (loginInfo.LoginType == LoginType.Default)
            {
                return new RegInfo
                {
                    PrimeTag = loginInfo.PrimeTag,
                    UserName = loginInfo.UserName,
                    Password = loginInfo.Password,
                };
            }
            else
            {
                return new RegInfo
                {
                    PrimeTag = loginInfo.PrimeTag,
                    LoginType = loginInfo.LoginType,
                    OpenId = loginInfo.OpenId,
                    AccessToken = loginInfo.AccessToken,
                    AuthorizationCode = loginInfo.AuthorizationCode,
                };
            }
        }

        /// <summary>
        /// 转为登陆请求信息
        /// </summary>
        /// <returns></returns>
        public LoginInfo ToLoginInfo()
        {
            if (this.LoginType == LoginType.Default)
                return new LoginInfo(this.UserName, this.Password, null) { PrimeTag = this.PrimeTag };

            return new LoginInfo(this.LoginType, this.OpenId, this.AccessToken, null) { PrimeTag = this.PrimeTag };
        }
    }

    /// <summary>
    /// 用户注册结果
    /// </summary>
    public sealed class RegResult
    {
        public RegResult(string primeTag)
        {
            this.PrimeTag = primeTag;
            this.IsSucc = false;
            this.Error = "注册失败";
            this.UserId = -1;
            this.UserName = null;
            this.SecurityToken = null;
        }

        /// <summary>
        /// 注册的区域（帐套）
        /// </summary>
        public string PrimeTag
        {
            get;
            set;
        }
        /// <summary>
        /// 是否注册成功
        /// </summary>
        public bool IsSucc
        {
            get;
            set;
        }
        /// <summary>
        /// 错误信息
        /// </summary>
        public string Error
        {
            get;
            set;
        }
        /// <summary>
        /// 注册用户的id
        /// </summary>
        public int UserId
        {
            get;
            set;
        }
        /// <summary>
        /// 注册用户的用户名
        /// </summary>
        public string UserName
        {
            get;
            set;
        }
        /// <summary>
        /// 注册用户的昵称
        /// </summary>
        public string NickName
        {
            get;
            set;
        }
        /// <summary>
        /// 注册用户的姓名
        /// </summary>
        public string RealName
        {
            get;
            set;
        }
        /// <summary>
        /// 头像
        /// </summary>
        public string HeadImgURL
        {
            get;
            set;
        }
        /// <summary>
        /// 是否登陆成功
        /// </summary>
        public bool IsLogin
        {
            get { return this.IsSucc && !string.IsNullOrEmpty(this.SecurityToken); }
        }
        /// <summary>
        /// 登陆令牌
        /// </summary>
        public string SecurityToken
        {
            get;
            set;
        }
    }
}